package com.longcheer.weibo.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.longcheer.weibo.vo.User;
import com.weibo.net.AccessToken;
import com.weibo.net.RequestToken;

public class WeiboConfig {
    private static final String TAG = "PW_WeiboConfig";
    
    public static RequestToken requestToken;
    public static AccessToken accessToken;
    
    public static String weiboUser;
    public static String weiboPasswod;
    
    public static void readConfig(Context context) {
        SharedPreferences sp = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE);
        String token = sp.getString("access_token_token", null);
        String secret = sp.getString("access_token_secret", null);
        if (token == null || secret == null) {
            accessToken = null;
        } else {
            accessToken = new AccessToken(token, secret);
            Log.i(TAG, String.format("Get accessToken[%s:%s]", token, secret));
        }

        token = sp.getString("request_token_token", null);
        secret = sp.getString("request_token_secret", null);
        if (token == null || secret == null) {
            requestToken = null;
        } else {
            requestToken = new RequestToken(token, secret);
            Log.i(TAG, String.format("Get requestToken[%s:%s]", token, secret));
        }
//        SharedPreferences sp = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE);
//        String accessTokenText = sp.getString("access_token", null);
//        if (accessTokenText == null) {
//            accessToken = null;
//        } else {
//            try {
//                accessToken = new MyAccessToken(accessTokenText);
//                Log.i(TAG, String.format("Get accessToken[%s]", accessTokenText));
//            } catch (JSONException e) {
//                e.printStackTrace();
//                accessToken = null;
//            }
//        }
    }
    
    public static void putAccessToken(Context context, AccessToken accessToken) {
        WeiboConfig.accessToken = accessToken;
        Editor edit = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE).edit();
        edit.putString("access_token_token", accessToken.getToken());
        edit.putString("access_token_secret", accessToken.getSecret());
        edit.commit();
        Log.i(TAG, String.format("Put requestToken[%s:%s]", accessToken.getToken(), accessToken.getToken()));
//        WeiboConfig.accessToken = accessToken;
//        Editor edit = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE).edit();
//        edit.putString("access_token", accessToken.getJsonText());
//        edit.commit();
//        Log.i(TAG, String.format("Put accessToken[%s]", accessToken.getJsonText()));
    }
//    
    public static void putRequestToken(Context context, RequestToken requestToken) {
        WeiboConfig.requestToken = requestToken;
        Editor edit = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE).edit();
        edit.putString("request_token_token", requestToken.getToken());
        edit.putString("request_token_secret", requestToken.getSecret());
        edit.commit();
        Log.i(TAG, String.format("Put requestToken[%s:%s]", requestToken.getToken(), requestToken.getToken()));
    }
    
    public static void putWeiboUser(Context context, String user) {
        WeiboConfig.weiboUser = user;
        Editor edit = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE).edit();
        edit.putString("weibo_user", user);
        edit.commit();
        Log.i(TAG, "Put weibo user:" + user);
    }
    
    public static void putWeiboPassword(Context context, String pwd) {
        WeiboConfig.weiboPasswod = pwd;
        Editor edit = context.getSharedPreferences("PhoneWeibo", Context.MODE_PRIVATE).edit();
        edit.putString("weibo_password", pwd);
        edit.commit();
        Log.i(TAG, "Put weibo password:" + pwd);
    }
}
